package pers.zc.activiti.service;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import pers.zc.activiti.TaskInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ActivitiService {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 部署流程
     * @param processName
     * @return
     */
    public ProcessDefinition deployProcess(String processName) {
        Deployment deploy = repositoryService.createDeployment().name(processName).addClasspathResource("process/" + processName + ".bpmn").deploy();
        logger.debug("Deployment id：{}", deploy.getId());
        //ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        //logger.debug("ProcessDefinition id：{}", definition.getId());
        return null;
    }

    /**
     * 启动流程
     * @param key
     * @return
     */
    public ProcessInstance startProcessByKey(String key, Map<String, Object> map) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(key, map);
        logger.debug("ProcessInstance id：{}", instance.getId());
        return instance;
    }

    public ProcessInstance startProcessById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        ProcessInstance instance = runtimeService.startProcessInstanceById(id);
        logger.debug("ProcessInstance id：{}", instance.getId());
        return instance;
    }

    public List<TaskInfo> getTasksByAssignee(String assignee) {
        if (StringUtils.isBlank(assignee)) {
            return null;
        }
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
        if (tasks == null) {
            return null;
        }
        List<TaskInfo> infos = new ArrayList<>();
        for (Task task : tasks) {
            infos.add(new TaskInfo(task.getId(), task.getName()));
        }
        return infos;
    }

    public List<TaskInfo> getTasksByGroup(String group) {
        if (StringUtils.isBlank(group)) {
            return null;
        }
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(group).list();
        if (tasks == null) {
            return null;
        }
        List<TaskInfo> infos = new ArrayList<>();
        for (Task task : tasks) {
            infos.add(new TaskInfo(task.getId(), task.getName()));
        }
        return infos;
    }

    public List<TaskInfo> getTasks(String assigneeOrGroup) {
        if (StringUtils.isBlank(assigneeOrGroup)) {
            return null;
        }
        List<Task> tasks = taskService.createTaskQuery().taskCandidateOrAssigned(assigneeOrGroup).list();
        if (tasks == null) {
            return null;
        }
        List<TaskInfo> infos = new ArrayList<>();
        for (Task task : tasks) {
            infos.add(new TaskInfo(task.getId(), task.getName()));
        }
        return infos;
    }

    public void completeTask(String taskId, Map<String, Object> map) {
        if (StringUtils.isBlank(taskId) || map == null) {
            throw new RuntimeException("Params cannot be empty");
        }
        taskService.complete(taskId, map);
    }

    public void completeTask(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            return;
        }
        taskService.complete(taskId);
    }


    public void deleteDeployment(String id) {
        repositoryService.deleteDeployment(id);
    }
}
